﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;

public partial class Modules_Module_NewsManage : System.Web.UI.UserControl
{
    private void Bind()
    {
        MysqlHelp sql = new MysqlHelp();
        string sqlconn = "select * from news where enable = 1 order by isTop DESC, publishtime DESC";
        this.GridView1.DataSource = sql.GetDatabysql(sqlconn);
        this.GridView1.DataKeyNames = new System.String[] { "id" };
        this.GridView1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["AsAdmin"] = "Admin";
        MultiView_NewsManage.SetActiveView(View_ModifyNews);
        if(!IsPostBack)
        {
            Bind();
        }
       
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        MysqlHelp Adupdate = new MysqlHelp();
        string sqlconn = "update news set enable=0 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";
        int back = Adupdate.Runsql(sqlconn);
        if (back != -1)
        {
            Response.Write("<script>alert('新闻删除成功！')</script>");
            Bind();
        }
        else
        {
            Response.Write("<script>alert('新闻删除失败！');history.goback(-1);</script>");
        }
        this.GridView1.DataBind();
    }
    protected void Button_NewsSubmit_Click(object sender, EventArgs e)
    {
      string AsTitle = TextBox_Title.Text.Trim();
       string AsContext = TextBox_Context.Text.Trim();
        if(AsTitle!=""&&AsContext!="")
        {
            string AsTime = DateTime.Now.ToString("");
            string AsCheckB = "0";
            string AsAdmin=Session["AsAdmin"].ToString();
            if (CheckBox_Top.Checked == true)
            {
                AsCheckB = "1";
            }
            MysqlHelp NewInsert=new MysqlHelp();
            string Assql = "insert into news (title,message,publishtime,publisherID,isTop,enable) values ('" + AsTitle + "','" + AsContext + "','" + AsTime + "','" + AsAdmin + "','" + AsCheckB + "',1)";
             int back = NewInsert.Runsql(Assql);     
            if(back!=-1)
            {
                Response.Write("<script>alert('新闻添加成功！')</script>");
                MultiView_NewsManage.SetActiveView(View_ModifyNews);
                Bind();
            }
            else
            {
                Response.Write("<script>alert('新闻添加失败！');history.goback(-1);</script>");
            }
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bind();
        ((CheckBox)(GridView1.Rows[e.NewEditIndex].FindControl("checkistop"))).Enabled = true;
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        MysqlHelp dataupdate = new MysqlHelp();
        string updateTime = DateTime.Now.ToString();
        string newtitle = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
        string newmessage=((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
        string top="0";
        CheckBox tmp = (CheckBox)GridView1.Rows[e.RowIndex].Cells[4].FindControl("checkistop");
        if(tmp.Checked==true)
        {
            top = "1";
        }
        string Updatestr = "update news set title='" +newtitle +"',message='"+newmessage+"',publishtime='"+updateTime+"',isTop='"+top+"'where id='"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";
       int back = dataupdate.Runsql(Updatestr);
       if (back != -1)
       {
           Response.Write("<script>alert('新闻修改成功！')</script>");
       }
       else
       {
           Response.Write("<script>alert('新闻修改失败！');history.goback(-1);</script>");
       }
       GridView1.EditIndex = -1;
       Bind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        Bind();
    }
    protected void AsPostNew_Click(object sender, EventArgs e)
    {
        MultiView_NewsManage.SetActiveView(View_PostNews);
    }
}
